package com.example.demo.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.service.OSSFileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Package： cn.liuliang.quickdinesysstore.controller
 * @Author： liuliang
 * @CreateTime： 2020/10/26 - 9:11
 * @Description：
 */
@Api(value = "OSS文件上传管理", tags = "OSS文件上传管理")
@RestController
@RequestMapping("/oss/file")
public class OSSFileController {

    @Autowired
    private OSSFileService ossFileService;


    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Map<String,Object> upload(
        @ApiParam(value = "文件对象", required = true) @RequestParam("file") MultipartFile file,
        @ApiParam(value = "文件上传的模块名称（目录名称）", required = true) @RequestParam(value="module",required = true) String module) {
    	Map<String,Object> map = new HashMap<>();
        String uploadUrl = "";
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        uploadUrl = ossFileService.upload(inputStream, module, file.getOriginalFilename());
        map.put("message", "上传成功了");
        map.put("url",uploadUrl);
        return map;
    }

    @ApiOperation("文件删除")
    @GetMapping("/remove")
    public Map<String,Object> removeFile(
        @ApiParam(value = "要删除的文件路径", required = true) @RequestParam String url) {
        ossFileService.removeFile(url);
        Map<String,Object> map = new HashMap<>();
        map.put("message","删除成功了");
        return map;
    }


}
